package com.lv.leyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lv.leyou.common.dto.BaseDTO;
import com.lv.leyou.common.dto.PageDTO;
import com.lv.leyou.common.exception.LyException;
import com.lv.leyou.dto.BrandDTO;
import com.lv.leyou.entity.Brand;
import com.lv.leyou.entity.CategoryBrand;
import com.lv.leyou.mapper.BrandMapper;
import com.lv.leyou.service.BrandService;
import com.lv.leyou.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public BrandDTO queryBrandById(Integer id) {
        Brand brand = getById(id);
        Long id1 = brand.getId();
        String image = brand.getImage();
        Character letter = brand.getLetter();
        String name = brand.getName();
        BrandDTO brandDTO = new BrandDTO();
        brandDTO.setId(id1);
        brandDTO.setImage(image);
        brandDTO.setName(name);
        brandDTO.setLetter(letter);

        return brandDTO;
    }

    @Override
    public List<Brand> queryBrandByIds(List<Long> ids) {
        List<Brand> brandList = listByIds(ids);
        return brandList;
    }

    @Override
    public PageDTO<BrandDTO> queryPage(Integer page, Integer rows, String key) {
     //分页信息的健壮性
        Math.min(page, 100);
        Math.max(rows, 5);
        //分页
        Page<Brand> info = new Page<>(page, rows);
        //判断key是否存在
        boolean blank = StringUtils.isNoneBlank(key);
        //如果key存在，添加Like和eq的查询条件,否则不添加
        query().like(blank, "name", key).or().eq(blank, "letter", key).page(info);
        //封装结果
        List<Brand> list = info.getRecords();


        return new PageDTO<>(info.getTotal(), info.getPages(), BrandDTO.convertEntityList(list));
    }

    @Override
    public List<BrandDTO> queryBrandByCategoryId(Integer id) {

        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("category_id", id).list();
        if (CollectionUtils.isEmpty(categoryBrandList)) {
            return Collections.emptyList();
        }
        //获取品牌id的集合
        List<Long> list = categoryBrandList.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());
        //根据品牌id集合查询商品集合
        List<Brand> brandList = listByIds(list);
        //转换
        List<BrandDTO> brandDTOList = BrandDTO.convertEntityList(brandList);

        return brandDTOList;
    }
    @Transactional
    @Override
    public void saveBrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        save(brand);
        //第一种实现
/*        //新增品牌的同时，还要新增中间表。
        //获取分类id集合
        List<Long> categoryIds = brandDTO.getCategoryIds();
        //获取品牌id，品牌对应分类一对多。
        Long brandId = brand.getId();
          for (Long categoryId : categoryIds) {
            CategoryBrand categoryBrand=new CategoryBrand();
            categoryBrand.setBrandId(brandId);
            categoryBrand.setCategoryId(categoryId);
        }*/
/*    //第二种实现
        //获取分类id
        List<Long> categoryIds = brandDTO.getCategoryIds();
        //封装中间表对象集合
        Long brandId = brand.getId();
        List<CategoryBrand> categoryBrandList = new ArrayList<>();
        for (Long categoryId : categoryIds) {
            //创建中间表对象
            CategoryBrand categoryBrand = CategoryBrand.of(categoryId, brandId);
            categoryBrandList.add(categoryBrand);
        }*/
        //第三种实现
        //封装中间表对象的集合
        List<CategoryBrand> list =brandDTO.getCategoryIds().stream().map(id->CategoryBrand.of(id,brand.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(list);
    }
    @Transactional
    @Override
    public void updateBrand(BrandDTO brandDTO) {
        //更新品牌
        //获取品牌
        Brand brand = brandDTO.toEntity(Brand.class);
        //更新品牌
        updateById(brand);
        //更新就是先删除再添加，根据品牌id删除中间表，
        Long brandId = brand.getId();
        QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);
        boolean remove = categoryBrandService.remove(queryWrapper);
        if (!remove) {
            throw new LyException(500, "更新品牌失败，删除中间表出错");
        }
   /*     List<CategoryBrand> list = new ArrayList<>();
        //重写添加中间表
        List<Long> categoryIds = brandDTO.getCategoryIds();
        for (Long categoryId : categoryIds) {
            CategoryBrand categoryBrand=CategoryBrand.of(categoryId,brandId);
            list.add(categoryBrand);
        }*/
        //封装中间表对象的集合
        List<CategoryBrand> collect = brandDTO.getCategoryIds().stream().map(id -> CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(collect);

    }
    @Transactional
    @Override
    public void deleteByBrandId(Integer id) {
        boolean removeById = removeById(id);

        //根据品牌id删除中间表
        boolean removeById1 = categoryBrandService.removeById(id);
    }


}

